﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Base.Cache;
using CJAtech.BBS.Web.Filters;
using CJAtech.BBS.Process;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Controllers
{
    [SSOAuthorFilter(PowerType.CheckLogin)]
    public partial class SSOBBSController : BBSController
    {

        public override UserInfo GetUserInfo()
        {
            return ViewBag.UserInfo as UserInfo;
        }

        [SSOAuthorFilter(PowerType.None)]
        public ActionResult LoginOut()
        {
            SSOProcess objSSOProcess = new SSOProcess(this.CurrentCommunityConfig);
            string token = CookieHelper.GetCookieValue(this.CurrentCommunityConfig.ConfigCode + "SSOToken");
            InvokeResult objInvokeResult = new InvokeResult();
            if (token.IsNull())
            {
                objInvokeResult.ResultCode = "TokenNull";
                objInvokeResult.ResultMessage = "注销失败，Token为空";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            objSSOProcess.LoginOut(token);
            objInvokeResult.ResultMessage = "注销成功";
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        [SSOAuthorFilter(PowerType.None)]
        public ActionResult Target(string token, string sign, string targetUrl)
        {
            if (targetUrl.IsNull())
            {
                return Http404();
            }
            targetUrl = targetUrl.DecodeUrl();

            BizCommunityConfig objBizCommunityConfig = new BizCommunityConfig();
            CommunityConfig objCommunityConfig = objBizCommunityConfig.GetRecordCache("", TimeSpan.FromSeconds(10), "ConfigCode=@ConfigCode", new { ConfigCode = this.CurrentCommunityConfig.ConfigCode });
            SSOProcess objSSOProcess = new SSOProcess(objCommunityConfig);

            if (!objSSOProcess.ValidTicket())
            {
                return Content("请求不合法");
            }

            UserInfo objUserInfo = null;
            if (string.Equals(objCommunityConfig.ConfigCode, "benz", StringComparison.OrdinalIgnoreCase) || ConfigHelper.GetBoolValue("IsTest", false))
            {
                objUserInfo = objSSOProcess.GetUserInfo(token);
            }
            else
            {
                objUserInfo = objSSOProcess.GetUserInfoConvert(token);
            }
            if (objUserInfo == null)
            {
                return Redirect(objSSOProcess.CreateRedirectUrl(targetUrl));
            }

            //LogHelper.WriteLog("跟踪帐号信息", objSSOLoginInfo.JsonSerialize());

            //CookieHelper.SetCookieValue(objSSOLoginInfo.JsonSerialize().EncodeBase64(), this.BBSName + "BBSUserTicket");

            CookieHelper.SetCookieValue(token, this.CurrentCommunityConfig.ConfigCode + "SSOToken");

            return Redirect(targetUrl);
        }
    }
}
